/**
  ******************************************************************************
  * File Name          : gpio.c
  * Description        : This file provides code for the configuration
  *                      of all used GPIO pins.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "gpio.h"
#include "adc.h"
#include "usart.h"
#include <stdio.h>
/* USER CODE BEGIN 0 */
extern uint8_t lock_sts;
extern void LED2_YELLOW_BLINKING(void);
extern void LED3_BLUE_OFF(void);
extern void LED4_GREEN_ON(void);
/* USER CODE END 0 */

/*----------------------------------------------------------------------------*/
/* Configure GPIO                                                             */
/*----------------------------------------------------------------------------*/
/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/** Configure pins as
        * Analog
        * Input
        * Output
        * EVENT_OUT
        * EXTI
*/
void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOF_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, LED4_G_Pin|LED3_b_Pin|LED2_Y_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(Relay_GPIO_Port, Relay_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pins : PBPin PBPin PBPin PBPin */
  GPIO_InitStruct.Pin = LED4_G_Pin|LED3_b_Pin|LED2_Y_Pin|Relay_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /*Configure GPIO pin : PtPin */
  GPIO_InitStruct.Pin = BUTTON_5D_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(BUTTON_5D_GPIO_Port, &GPIO_InitStruct);

  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI4_15_IRQn, 1, 0);
  HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);

}

/* USER CODE BEGIN 2 */

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	if(GPIO_Pin == BUTTON_5D_Pin)
	{
		//消抖处理
		HAL_Delay(100);
		//ADC校验
		HAL_ADCEx_Calibration_Start(&hadc);
		//开始ADC转换
		HAL_ADC_Start(&hadc);
		//等待ADC转换完成
		HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY);
		int key_value = HAL_ADC_GetValue(&hadc);
		key_value /= 100;
		printf("key_value = %d\n", key_value);
		
		while(HAL_GPIO_ReadPin(BUTTON_5D_GPIO_Port, BUTTON_5D_Pin) == GPIO_PIN_SET) ;//等待按键抬起
		
		switch(key_value)
		{
			case 25:
				if(1 == lock_sts)
				{
					HAL_GPIO_WritePin(Relay_GPIO_Port, Relay_Pin, GPIO_PIN_RESET);
					LED3_BLUE_OFF();
					LED4_GREEN_ON();
					lock_sts = 0;
					HAL_UART_Transmit(&huart2, (uint8_t *)"AT+NMGS=2,6364\r\n", 16, HAL_MAX_DELAY);
				}
				else
				{
					LED2_YELLOW_BLINKING();
				}
				break;
			default:
				puts("Just press can close.");
				HAL_UART_Transmit(&huart2, (uint8_t *)"AT+NMGS=2,0102\r\n", 16, HAL_MAX_DELAY);
		}
		
	}
		
}
/* USER CODE END 2 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
